package com.lovepick.fragment;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.lovepick.R;
import com.lovepick.adapter.MsgListItemAdapter;
import com.lovepick.bean.MsgListItem;
import com.lovepick.database.MsgListDbManager;
import com.lovepick.util.BaseUtils;

public class MsgListFragment extends Fragment
{
	private static final String TAG = "MsgListFragment";
	private ListView mListView;
	private ProgressBar mNoneBar;

	private List<MsgListItem> mMsgLists = null;
	private MsgListItemAdapter mAdapter = null;
	private MsgListDbManager mDbManager = null;
	private OnMsgListItemClickListener mCallBack = null;

	public interface OnMsgListItemClickListener
	{
		public void onMsgListItemClick(int userID, String nickName);
	}

	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		try
		{
			mCallBack = (OnMsgListItemClickListener) activity;
		}
		catch (ClassCastException e)
		{
			// TODO: handle exception
			throw new ClassCastException(activity.toString()
					+ " must implement OnMsgListItemClickListener");
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);
		mListView = (ListView) getActivity().findViewById(R.id.lv_msglist);
		mNoneBar = (ProgressBar) getActivity().findViewById(R.id.progb_none);
		mDbManager = new MsgListDbManager();
		new MsgListTask().execute();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.msglist, container, false);
	}

	@Override
	public void onPrepareOptionsMenu(Menu menu)
	{
		if (null == mMsgLists)
		{
			menu.findItem(R.id.menu_msglist_delete).setEnabled(false);
		}
		else
		{
			menu.findItem(R.id.menu_msglist_delete).setEnabled(true);
		}
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
	{
		inflater.inflate(R.menu.msglist, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.menu_refresh_msgs:
				new MsgListTask().execute();
				return true;

			case R.id.menu_msg_delete:
				deleteMsgs();
				return true;

			default:
				break;
		}

		return super.onOptionsItemSelected(item);
	}

	private void deleteMsgs()
	{
		Log.i(TAG, "delete all msg lists!");
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle(R.string.dialog_title_delete_confirm)
		.setMessage(R.string.dialog_delete_confirm)
		.setPositiveButton(R.string.delete, new DialogInterface.OnClickListener()
		{

			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				// TODO Auto-generated method stub
				mDbManager.deleteMsgListAll();
				if (mMsgLists != null)
				{
					mMsgLists.clear();
				}
				mAdapter.notifyDataSetChanged();
				mMsgLists = null;
			}
		})
		.setNegativeButton(R.string.cancel, null)
		.create().show();

	}

	public void refresh()
	{
		new MsgListTask().execute();
	}

	private AdapterView.OnItemClickListener mItemClickListener =
			new AdapterView.OnItemClickListener()
	{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			// TODO Auto-generated method stub
			int userID = mMsgLists.get(position).getUserID();
			String nickName = mMsgLists.get(position).getNickName();
			if (mMsgLists.get(position).getHasNew() == 1)
			{
				mMsgLists.get(position).setHasNew(0);
				mDbManager.updateMsgListItem(mMsgLists.get(position));
			}

			mCallBack.onMsgListItemClick(userID, nickName);
		}
	};

	@Override
	public void onResume()
	{
		Log.i(TAG, "---onResume---");
		super.onResume();
		new MsgListTask().execute();
	}

	@Override
	public void onPause()
	{
		Log.i(TAG, "---onPause---");
		super.onPause();
	}

	private class MsgListTask extends AsyncTask<Void, Void, List<MsgListItem>>
	{
		private final String TASK_TAG = "MsgListTask";

		@Override
		protected void onPreExecute()
		{
			mNoneBar.setVisibility(View.VISIBLE);
		}

		@Override
		protected List<MsgListItem> doInBackground(Void... params)
		{
			return mDbManager.getMsgListItems();
		}

		@Override
		protected void onPostExecute(List<MsgListItem> result)
		{
			Log.i(TASK_TAG, "## task finished ##");
			mNoneBar.setVisibility(View.GONE);
			if (null == result)
			{
				BaseUtils.showToast(getActivity(), "暂时没有新消息!");
				if (mMsgLists != null)
				{
					mMsgLists.clear();
					mAdapter.notifyDataSetChanged();
				}
			}
			else
			{
				mMsgLists = result;
				mAdapter = new MsgListItemAdapter(getActivity(), mMsgLists);
				mListView.setAdapter(mAdapter);
				mListView.setOnItemClickListener(mItemClickListener);
			}
		}

	}

}
